package com.example.demo.verticle

import com.example.demo.singleton.MySQLPoolHolder
import io.vertx.core.json.JsonObject
import io.vertx.kotlin.coroutines.CoroutineVerticle
import io.vertx.mysqlclient.MySQLConnectOptions
import io.vertx.mysqlclient.MySQLPool
import io.vertx.sqlclient.PoolOptions
import org.slf4j.LoggerFactory

import com.example.demo.dao.*
import io.vertx.core.eventbus.EventBus
import io.vertx.core.eventbus.Message
import io.vertx.core.json.JsonArray
import io.vertx.kotlin.coroutines.awaitResult
import io.vertx.sqlclient.Row
import io.vertx.sqlclient.RowSet

/**
 * @author Xu Haidong
 * @date 2023/4/20 10:52
 */
class DatasourceVerticle : CoroutineVerticle() {

  private val logger = LoggerFactory.getLogger(this.javaClass)

  override suspend fun start() {
    super.start()
    logger.info("初始化数据源")
    val localMap = vertx.sharedData().getLocalMap<String, JsonObject>("demo")
    val dsConfig = localMap["config"]?.getJsonObject("datasource")
    val connectOptions = MySQLConnectOptions(dsConfig?.getJsonObject("connect"))
    val poolOptions = PoolOptions(dsConfig?.getJsonObject("pool"))
    val mySQLPool = MySQLPool.pool(vertx, connectOptions, poolOptions)
    MySQLPoolHolder.setInstance(mySQLPool)
    val eventBus = vertx.eventBus()
    dbQueryEvent(eventBus, mySQLPool)
    dbQueryEventWithParams(eventBus, mySQLPool)
  }

  override suspend fun stop() {
    super.stop()
  }

}
